﻿using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

using EF;

namespace ConsoleApplication1
{
    class Program
    {
        static public void initLists(SPSite site)
        {
            SPList splistResult = null;
            SPView oViewResult = null;
            SPViewFieldCollection oViewFieldsResult = null;
            Guid guidResult = Guid.Empty;

            SPList splistHistory = null;
            SPView oViewHistory = null;
            SPViewFieldCollection oViewFieldsHistory = null;
            Guid guidHistory = Guid.Empty;

            string strViewQuery = string.Empty;
            string strViewQueryTemplate = string.Empty;
            StringCollection viewFields = null;

            try
            {
                splistResult = site.RootWeb.Lists.TryGetList(EF.EFInspectUniquePerm.Result_ListTitle);
                if (splistResult != null)
                    splistResult.Delete();
                //if (splistResult == null)
                {
                    guidResult = site.RootWeb.Lists.Add(EF.EFInspectUniquePerm.Result_ListTitle, EF.EFInspectUniquePerm.Result_ListDescription, SPListTemplateType.GenericList);
                    splistResult = site.RootWeb.Lists[guidResult];

                    splistResult.Fields.Add(EF.EFInspectUniquePerm.Column_Result_ObjectType, SPFieldType.Choice, true);
                    SPFieldChoice objSPFieldWorkflowType = (SPFieldChoice)splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_ObjectType];
                    objSPFieldWorkflowType.Choices.Add(EF.EFInspectUniquePerm.ObjectType.Web.ToString());
                    objSPFieldWorkflowType.Choices.Add(EF.EFInspectUniquePerm.ObjectType.List.ToString());
                    objSPFieldWorkflowType.Choices.Add(EF.EFInspectUniquePerm.ObjectType.Folder.ToString());
                    objSPFieldWorkflowType.Choices.Add(EF.EFInspectUniquePerm.ObjectType.Item.ToString());
                    objSPFieldWorkflowType.DefaultValue = EF.EFInspectUniquePerm.ObjectType.Web.ToString();
                    objSPFieldWorkflowType.Update();

                    splistResult.Fields.Add(EF.EFInspectUniquePerm.Column_Result_ModifiedByUser, SPFieldType.User, false);
                    splistResult.Fields.Add(EF.EFInspectUniquePerm.Column_Result_ModifiedTime, SPFieldType.DateTime, false);
                    SPFieldDateTime objSPFieldLastScanTime = (SPFieldDateTime)splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_ModifiedTime];
                    objSPFieldLastScanTime.DisplayFormat = SPDateTimeFieldFormatType.DateTime;
                    objSPFieldLastScanTime.Update();

                    splistResult.Fields.Add(EF.EFInspectUniquePerm.Column_Result_WebUrl, SPFieldType.Text, false);
                    splistResult.Fields.Add(EF.EFInspectUniquePerm.Column_Result_Link, SPFieldType.URL, true);

                    splistResult.Fields.Add(EF.EFInspectUniquePerm.Column_Result_UserRights, SPFieldType.Note, false);
                    SPFieldMultiLineText objSPFieldWebTitle = (SPFieldMultiLineText)splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_UserRights];
                    objSPFieldWebTitle.RichText = false;
                    objSPFieldWebTitle.RichTextMode = SPRichTextMode.Compatible;
                    objSPFieldWebTitle.Update();

                    splistResult.EnableAttachments = false;
                    splistResult.Update();

                    oViewResult = splistResult.DefaultView;
                    oViewFieldsResult = oViewResult.ViewFields;
                    oViewFieldsResult.Add(splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_ObjectType]);
                    oViewFieldsResult.Add(splistResult.Fields[SPBuiltInFieldId.Title]);
                    oViewFieldsResult.Add(splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_ModifiedByUser]);
                    oViewFieldsResult.Add(splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_ModifiedTime]);
                    oViewFieldsResult.Add(splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_WebUrl]);
                    oViewFieldsResult.Add(splistResult.Fields[EF.EFInspectUniquePerm.Column_Result_Link]);

                    oViewResult.TabularView = false;
                    oViewResult.Update();

                    viewFields = new StringCollection();
                    viewFields.Add(EF.EFInspectUniquePerm.Column_Result_ObjectType);
                    viewFields.Add(splistResult.Fields[SPBuiltInFieldId.Title].Title);
                    viewFields.Add(EF.EFInspectUniquePerm.Column_Result_ModifiedByUser);
                    viewFields.Add(EF.EFInspectUniquePerm.Column_Result_ModifiedTime);
                    viewFields.Add(EF.EFInspectUniquePerm.Column_Result_WebUrl);
                    viewFields.Add(EF.EFInspectUniquePerm.Column_Result_Link);

                    strViewQueryTemplate = @"<Eq><FieldRef Name='" + EF.EFInspectUniquePerm.Column_Result_ObjectType + @"' /><Value Type='Choice'>{0}</Value></Eq>";
                    strViewQueryTemplate = @"<Where>" + strViewQueryTemplate + @"</Where>";
                    strViewQuery = string.Format(strViewQueryTemplate, EF.EFInspectUniquePerm.ObjectType.Web);
                    splistResult.Views.Add(EF.EFInspectUniquePerm.View_Web, viewFields, strViewQuery, 30, true, false);
                    strViewQuery = string.Format(strViewQueryTemplate, EF.EFInspectUniquePerm.ObjectType.List);
                    splistResult.Views.Add(EF.EFInspectUniquePerm.View_List, viewFields, strViewQuery, 30, true, false);
                    strViewQuery = string.Format(strViewQueryTemplate, EF.EFInspectUniquePerm.ObjectType.Folder);
                    splistResult.Views.Add(EF.EFInspectUniquePerm.View_Folder, viewFields, strViewQuery, 30, true, false);
                    strViewQuery = string.Format(strViewQueryTemplate, EF.EFInspectUniquePerm.ObjectType.Item);
                    splistResult.Views.Add(EF.EFInspectUniquePerm.View_Item, viewFields, strViewQuery, 30, true, false);
                }

                //make sure the history list is already created
                splistHistory = site.RootWeb.Lists.TryGetList(EF.EFInspectUniquePerm.History_ListTitle);
                if (splistHistory != null)
                    splistHistory.Delete();

                guidHistory = site.RootWeb.Lists.Add(EF.EFInspectUniquePerm.History_ListTitle, EF.EFInspectUniquePerm.History_ListDescription, SPListTemplateType.GenericList);
                splistHistory = site.RootWeb.Lists[guidHistory];
                splistHistory.Fields.Add(EF.EFInspectUniquePerm.Column_History_WebCount, SPFieldType.Number, false);
                splistHistory.Fields.Add(EF.EFInspectUniquePerm.Column_History_ListCount, SPFieldType.Number, false);
                splistHistory.Fields.Add(EF.EFInspectUniquePerm.Column_History_FolderCount, SPFieldType.Number, false);
                splistHistory.Fields.Add(EF.EFInspectUniquePerm.Column_History_ItemCount, SPFieldType.Number, false);
                splistHistory.Fields[SPBuiltInFieldId.Title].Required = false;
                splistHistory.Fields[SPBuiltInFieldId.Title].Update();
                splistHistory.EnableAttachments = false;
                splistHistory.Update();

                oViewHistory = splistHistory.DefaultView;
                oViewFieldsHistory = oViewHistory.ViewFields;
                oViewFieldsHistory.Add(splistHistory.Fields[SPBuiltInFieldId.Modified].InternalName);
                oViewFieldsHistory.Delete(splistHistory.Fields[SPBuiltInFieldId.LinkTitle].InternalName);
                oViewFieldsHistory.Add(splistHistory.Fields[SPBuiltInFieldId.Title].InternalName);
                oViewFieldsHistory.Add(splistHistory.Fields[EF.EFInspectUniquePerm.Column_History_WebCount]);
                oViewFieldsHistory.Add(splistHistory.Fields[EF.EFInspectUniquePerm.Column_History_ListCount]);
                oViewFieldsHistory.Add(splistHistory.Fields[EF.EFInspectUniquePerm.Column_History_FolderCount]);
                oViewFieldsHistory.Add(splistHistory.Fields[EF.EFInspectUniquePerm.Column_History_ItemCount]);
                oViewHistory.Query = @"<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";
                //oViewHistory.TabularView = false;
                oViewHistory.Update();
            }
            catch (Exception ex)
            {
                logDebug(string.Format(@"EFInspectUniquePerm, initLists(), ex.Message={0}", ex.Message));
                logDebug(string.Format(@"EFInspectUniquePerm, initLists(), ex.StackTrace={0}", ex.StackTrace));

                throw;
            }
        }

        static public void logDebug(string strMessage)
        {
            Console.WriteLine(strMessage);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("begin.");
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite objSPSite = new SPSite(@"http://hvsp2010a:8080"))
                {
                    using (SPWeb objSPWeb = objSPSite.RootWeb)
                    {
                        try
                        {
                            initLists(objSPSite);
                            //initAdminPage(objSPSite);
                        }
                        catch (Exception ex)
                        {
                            logDebug(string.Format(@"ConsoleTest, Main(), ex.Message={0}", ex.Message));
                            logDebug(string.Format(@"ConsoleTest, Main(), ex.StackTrace={0}", ex.StackTrace));
                            throw;
                        }
                    }
                }

                //SPWebService contentService = SPWebService.ContentService;
                //contentService.WebConfigModifications.Add(GetConfigModification());
                //contentService.Update();
                //contentService.ApplyWebConfigModifications();
            });

            Console.WriteLine("compelte.");
            Console.ReadLine();
        }
    }
}
